package com.fanbuchi.fukua.manager;

import android.content.Context;

import com.fanbuchi.fukua.config.AppConfig;
import com.fanbuchi.fukua.greendao.DaoMaster;
import com.fanbuchi.fukua.greendao.DaoSession;

/**
 * Created by killerzhangsan on 2015/5/26.
 */
public class DaoManager {

   private static String dbName=AppConfig.rrkdDBName;

    private static DaoMaster daoMaster;
    private static DaoSession daoSession;

    public static DaoMaster getDaoMaster(Context context) {
        if (daoMaster == null) {
            DaoMaster.OpenHelper helper = new DaoMaster.DevOpenHelper(context, dbName, null);
            daoMaster = new DaoMaster(helper.getWritableDatabase());
        }
        return daoMaster;
    }


    public static DaoSession getDaoSession(Context context) {
        if (daoSession == null){
            if (daoMaster == null){
                daoMaster = getDaoMaster(context);
            }
            daoSession = daoMaster.newSession();
        }
        return daoSession;
    }

    public static void safeClose(){
        daoMaster.getDatabase().close();
    }
}
